import cv2

# 读取图片
image = cv2.imread('xiufuqian.jpg')

# 把图片转换到YCrCb色彩空间
yCrCb = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)

# 拆分色彩空间的通道
y, Cr, Cb = cv2.split(yCrCb)

# 应用自适应直方图均衡化
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
cl1 = clahe.apply(Cr)
cl2 = clahe.apply(Cb)

# 合并色彩通道
yCrCb_output = cv2.merge((y, cl1, cl2))

# 把色彩空间从YCrCb转回BGR
img_output = cv2.cvtColor(yCrCb_output, cv2.COLOR_YCrCb2BGR)

# 显示结果
cv2.imshow('Original Image', image)
cv2.imshow('Equalized Image', img_output)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 保存结果
cv2.imwrite('11.jpg', img_output)